package visao;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class SuporteEventos implements ActionListener, 
                                        WindowListener, MenuListener, Serializable{
    
  private ArrayList<ActionListener> actionLists;
  private ArrayList<WindowListener> windowLists;
  private ArrayList<MenuListener> menuLists;
  private Object source;

  public SuporteEventos(Object s){
    this.source = s;
    actionLists = new ArrayList<ActionListener>();
    windowLists = new ArrayList<WindowListener>();
    menuLists = new ArrayList<MenuListener>();
  }

  public SuporteEventos() {

  }
  
  public void setSource(Object o){
      this.source=o;
  }

  // <editor-fold defaultstate="collapsed" desc="Adiciona ouvintes">
  public void addActionOuvinte(ActionListener l) {
    actionLists.add(l);
  }

  public void addWindowOuvinte(WindowListener l) {
     windowLists.add(l);
  }

  public void addMenuOuvinte(MenuListener l) {
     menuLists.add(l);
  }//</editor-fold>
  
  public void removeActionOuvinte(ActionListener l) {
    actionLists.remove(l);
  }

  // <editor-fold defaultstate="collapsed" desc="Sobrescreve Actionevents">                          

  @Override
  public void actionPerformed(ActionEvent e) {
    disparaActionEvent(e);
  }

  private void disparaActionEvent(ActionEvent e) {
    for (ActionListener l: actionLists) {
      l.actionPerformed(new ActionEvent(source, ActionEvent.ACTION_PERFORMED, e.getActionCommand()));
    }
  }//</editor-fold>

  // <editor-fold defaultstate="collapsed" desc="Sobrescreve WindowEvents">
    @Override
    public void windowOpened(WindowEvent e) {
    }

    @Override
    public void windowClosing(WindowEvent e) {
        this.disparaWindowEvent(e);
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }
    
    private void disparaWindowEvent(WindowEvent e) {
        for (WindowListener l: windowLists) {
            l.windowClosing(new WindowEvent((Window) source, WindowEvent.WINDOW_CLOSING));
        }
    }//</editor-fold>
    
  // <editor-fold defaultstate="collapsed" desc="Sobrescreve Actionevents">
    @Override
    public void menuDeselected(MenuEvent e) {
    }

    @Override
    public void menuCanceled(MenuEvent e) {
    }

    private void disparaMenuEvent(MenuEvent e) {
        for (MenuListener l: menuLists) {
            l.menuSelected(new MenuEvent(e.getSource()));
        }
    }//</editor-fold>
    
    @Override
    public void menuSelected(MenuEvent e) {
        disparaMenuEvent(e);
    }


}
